﻿namespace Guia.Elements
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows.Automation;
    using Guia.Config;
    using Guia.Locator;

    /// <summary>
    /// This class represents a (WinForms) SaveFileDialog.
    /// </summary>
    public class SaveFileDialog : UiElement
    {
        /// <summary>
        /// Initializes a new instance of the SaveFileDialog class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating the SaveFileDialog.</param>
        public SaveFileDialog(AutomationElement automationElement)
            : base(automationElement)
        {
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.Window).And(SearchCondition.ByClassName("#32770")); }
        }

        /// <summary>
        /// Sets the FilePath. 
        /// </summary>
        private string FilePath
        {
            set
            {
                ComboBox comboBox = this.Get<ComboBox>("1148");
                TextBox textBox = comboBox.Get<TextBox>("1148");
                textBox.Value = value;
            }
        }

        /// <summary>
        /// Saves the given file.
        /// </summary>
        /// <param name="filePath">The path of the desired file.</param>
        /// <exception cref="ArgumentNullException">This exception is thrown if filePath is null.</exception>
        public void Save(string filePath)
        {
            Contract.Requires<ArgumentNullException>(filePath != null);
            this.FilePath = filePath;
            this.Save();
        }

        /// <summary>
        /// Cancels the dialog.
        /// </summary>
        public void Cancel()
        {
            this.Get<Button>("2").Invoke();
        }

        /// <summary>
        /// Save the given file.
        /// </summary>
        private void Save()
        {
            this.Get<Button>("1").Invoke();
        }
    }
}
